require 'openssl'
# OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
# require 'win32ole'
require 'open-uri'
require 'net/http'
include Net

$threads = []
def create_dir(path)
    if(File.exist?(path))
        return path
    else
        test = Dir.mkdir(path)
        if(test)
            return path
        else
            return false
        end
    end
end
def download_img(path,img_url,item_name)
    $threads << Thread.new do
        puts "imgurl >>> " + img_url
        puts "path >>> " + path

        fw = open("fail-log.txt","a")
        n = 0
        if(img_url.strip != "")
            begin
                data=open(img_url, {ssl_verify_mode:OpenSSL::SSL::VERIFY_NONE, read_timeout: 60, open_timeout: 20}){|f|f.read}
                open(path,"wb"){|f|f.write(data)}
            rescue
                puts "open url  err @@@@@@" + path
                fw.puts item_name + " ===> open url  err"
                n += 1
                retry if n < 3
            end
        else
            puts "@@@@@@=====> imgurl is null"
            fw.puts item_name + " ===> imgurl is null"
        end

        fw.close
        sleep(3)
	end
end
def each_row(list)
	for item in list
        item.force_encoding("utf-8")
        data_row=item.split(',')
        id=data_row[0]
        img_main_url=data_row[2]
        img_another_url=data_row[3]
        title=data_row[0]
        filename = title.gsub(/\s|\+|\/|\\|\*|\:|\?|\"|\<|\>|\|/,"")
        
        create_dir("pic_downloads")
        dir=create_dir("pic_downloads/" + filename)

        path1 =  dir + "/" + id + ".jpg"
        # path2 =  dir + "/" + id + "_another" + ".jpg"

        download_img(path1, img_main_url, title)
        # download_img(path2, img_another_url, title)
    end
end
def get_list(file)
	io_r=File.open(file,"r")
	list= io_r.readlines
	return list
end
#-----------------------------
fw = open("fail-log.txt","w")
fw.print ""
fw.close
list =get_list("data.csv")
list.shift()
each_row(list)

$threads.each { |t|t.join }